using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using System;

namespace DotNetNuke.Modules.DNNWeather.Data
{
    /// <summary>
    /// SQL Server implementation of the abstract DataProvider class
    ///
    /// This concreted data provider class provides the implementation of the abstract methods from
    /// data dataprovider.cs
    ///
    /// In most cases you will only modify the Public methods region below.
    /// </summary>
    public class SqlDataProvider : DataProvider
    {
        #region Private Members

        private const string ProviderType = "data";
        private const string ModuleQualifier = "DNNWeather_";

        private readonly ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private readonly string _connectionString;
        private readonly string _providerPath;
        private readonly string _objectQualifier;
        private readonly string _databaseOwner;

        #endregion Private Members

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlDataProvider" /> class.
        /// </summary>
        public SqlDataProvider()
        {
            // Read the configuration specific information for this provider
            Provider objProvider = (Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]);

            // Read the attributes for this provider

            //Get Connection string from web.config
            _connectionString = Config.GetConnectionString();

            if (string.IsNullOrEmpty(_connectionString))
            {
                // Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (!string.IsNullOrEmpty(_objectQualifier) && _objectQualifier.EndsWith("_", StringComparison.Ordinal) == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (!string.IsNullOrEmpty(_databaseOwner) && _databaseOwner.EndsWith(".", StringComparison.Ordinal) == false)
            {
                _databaseOwner += ".";
            }
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }

        /// <summary>
        /// Gets the provider path.
        /// </summary>
        /// <value>The provider path.</value>
        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }

        /// <summary>
        /// Gets the object qualifier.
        /// </summary>
        /// <value>The object qualifier.</value>
        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }

        /// <summary>
        /// Gets the database owner.
        /// </summary>
        /// <value>The database owner.</value>
        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }

        /// <summary>
        /// Gets the name prefix.
        /// </summary>
        /// <value>The name prefix.</value>
        private string NamePrefix
        {
            get { return DatabaseOwner + ObjectQualifier + ModuleQualifier; }
        }

        #endregion Properties

        #region Private Methods

        private static object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion Private Methods

        #region Public Methods

        //public override IDataReader GetItem(int itemId)
        //{
        //    return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "spGetItem", itemId);
        //}

        //public override IDataReader GetItems(int userId, int portalId)
        //{
        //    return SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "spGetItemsForUser", userId, portalId);
        //}

        #endregion Public Methods
    }
}